﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Portal.Models;
using Best.SmartCms.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace Best.SmartCms.Portal.Components
{
    [ViewComponent(Name = "ProductTuijian")]
    public class ProductTuijianViewComponent : ViewComponent
    {
        private readonly ChannelGroupService _channelGroupService;
        private readonly ChannelService _channelService;
        private readonly IMemoryCache _cache;

        public ProductTuijianViewComponent(IMemoryCache memoryCache, ChannelGroupService channelGroupService, ChannelService channelService)
        {
            _channelGroupService = channelGroupService;
            _channelService = channelService;
            _cache = memoryCache;
        }

        public IViewComponentResult Invoke(long channelId)
        {
            var channel = _channelService.Info(channelId);
            var currentChannels = _channelService.ListByGroupId(channel.ChannelGroupId);
            var group = _channelGroupService.Info(channel.ChannelGroupId);

            var model = new MenuViewModel
            {
                GroupId = group.Id,
                GroupName = group.Name,
                Children = currentChannels.OrderBy(p=>p.OrderNo).Select(p=>new MenuItem{ Id = p.Id,Name = p.Name,Url=p.ControllerName, IsCurrentMenu = p.Id == channelId }).ToList()
            };

            return View(model);
        }
    }
}
